﻿using Hrm.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoMapper.QueryableExtensions;
using AutoMapper;

namespace Hrm.Services
{
    public class PortfolioService : BaseService<Portfolio>, IPortfolioService
    {
        IPortfolioStudioDbRepository _portfolioStudioDbRepository;
        IImageStudioDbRepository _imageStudioDbRepository;
        IDictionaryPermissionDbRepository _dictionaryPermissionDbRepository;
        public PortfolioService(IPortfolioStudioDbRepository portfolioStudioDbRepository, IImageStudioDbRepository imageStudioDbRepository, IDictionaryPermissionDbRepository dictionaryPermissionDbRepository)
            : base(portfolioStudioDbRepository)
        {
            this._portfolioStudioDbRepository = portfolioStudioDbRepository;
            this._imageStudioDbRepository = imageStudioDbRepository;
            this._dictionaryPermissionDbRepository = dictionaryPermissionDbRepository;
        }

        public List<PortfolioDto> GetPortfolios()
        {
            var result = this.GetAllAbledAndOrderByCreatetime().Project().To<PortfolioDto>().ToList();
            var portfolioIds = result.Select(p => p.Id);
            var imageList = this._imageStudioDbRepository.GetList(p => portfolioIds.Contains(p.DelongId) && p.DelongType == "Portfolio");
            result.ForEach(p => p.ImageUrl = imageList.Any(q => q.DelongId == p.Id) ? imageList.FirstOrDefault(q => q.DelongId == p.Id).Url : "");
            result.ForEach(p => p.ImageId = imageList.Any(q => q.DelongId == p.Id) ? imageList.FirstOrDefault(q => q.DelongId == p.Id).Id : 0);
            return result;
        }

        public List<PortfolioDto> TopPortfolios(int topNum)
        {
            var result = this.GetAllAbledAndOrderByCreatetime().Take(topNum).Project().To<PortfolioDto>().ToList();
            var portfolioIds = result.Select(p => p.Id);
            var imageList = this._imageStudioDbRepository.GetList(p => portfolioIds.Contains(p.DelongId) && p.DelongType == "Portfolio");
            result.ForEach(p => p.ImageUrl = imageList.Any(q => q.DelongId == p.Id) ? imageList.FirstOrDefault(q => q.DelongId == p.Id).Url : "");
            result.ForEach(p => p.ImageId = imageList.Any(q => q.DelongId == p.Id) ? imageList.FirstOrDefault(q => q.DelongId == p.Id).Id : 0);
            return result;
        }

        public List<PortfolioTypeDto> GetPortfolioTypes()
        {
            var result = this._dictionaryPermissionDbRepository.GetList(p => p.Type == "PortfolioType").Select(p => new PortfolioTypeDto() { Name = p.Name, Code = p.Code }).ToList();
            return result;
        }


        public PortfolioDto GetPortfolio(int id)
        {
            var result = Mapper.Map<PortfolioDto>(this.GetById(id));
            var image = this._imageStudioDbRepository.GetList(p => p.DelongId == id && p.DelongType == "Portfolio").FirstOrDefault();
            result.ImageUrl = image != null ? image.Url : "";
            result.ImageId = image != null ? image.Id : 0;
            return result;
        }

        public List<PortfolioDto> GetRelatedPortfolios(int id)
        {
            var currentModel = this.GetById(id);
            var result = this.GetListAbledAndOrderByCreatetime(p => p.Id != id && p.Type == currentModel.Type).Take(4).Project().To<PortfolioDto>().ToList();
            var portfolioIds = result.Select(p => p.Id);
            var imageList = this._imageStudioDbRepository.GetList(p => portfolioIds.Contains(p.DelongId) && p.DelongType == "Portfolio");
            result.ForEach(p => p.ImageUrl = imageList.Any(q => q.DelongId == p.Id) ? imageList.FirstOrDefault(q => q.DelongId == p.Id).Url : "");
            result.ForEach(p => p.ImageId = imageList.Any(q => q.DelongId == p.Id) ? imageList.FirstOrDefault(q => q.DelongId == p.Id).Id : 0);
            return result;
        }


        public bool AddPortfolioTypes(PortfolioTypeDto model)
        {
            var dic = new Dictionary()
            {
                Code = model.Code,
                Name = model.Name,
                Type = Constant.DictionaryPortfolioType,
                Description = model.Name,
                Order = 0,
                IsEnable = true,
                CreateDatetime = DateTime.Now,
            };
            return this._dictionaryPermissionDbRepository.Insert(dic) > 0;
        }

        public bool DeletePortfolioTypes(int id)
        {
            return this._dictionaryPermissionDbRepository.Delete(id) > 0;
        }

        public bool AddPortfolio(PortfolioDto model)
        {
            var addModel = Mapper.Map<Portfolio>(model);
            this._portfolioStudioDbRepository.Insert(addModel);
            this._imageStudioDbRepository.Insert(new Image()
            {
                Name = model.Name,
                Description = model.Name,
                DelongId = addModel.Id,
                DelongType = "Portfolio",
                Type = model.Type,
                Url = model.ImageUrl,
                IsEnable = true,
                CreateDatetime = DateTime.Now
            });
            return true;
        }

        public bool UpdatePortfolio(PortfolioDto model)
        {
            var updateModel = this._portfolioStudioDbRepository.GetById(model.Id);
            updateModel.Name = model.Name;
            updateModel.Description = model.Description;
            updateModel.Type = model.Type;
            updateModel.FinishDate = model.FinishDate;
            updateModel.SkillsUsed = model.SkillsUsed;
            updateModel.DemoUrl = model.DemoUrl;
            if (!string.IsNullOrWhiteSpace(model.ImageUrl))
            {
                var updateDicModel = this._imageStudioDbRepository.GetById(model.ImageId);
                updateDicModel.Name = model.Name;
                updateDicModel.Description = model.Name;
                updateDicModel.Type = model.Type;
                updateDicModel.Url = model.ImageUrl;
                this._imageStudioDbRepository.Update(updateDicModel,false);
            }
            return this._portfolioStudioDbRepository.Update(updateModel) > 0;
        }

        public bool DeletePortfolio(int id, int imageId = 0)
        {
            if (imageId != 0)
            {
                _imageStudioDbRepository.Delete(imageId);
            }
            return this._portfolioStudioDbRepository.Delete(id) > 0;
        }
    }
}
